package com.hermit.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hermit.system.dao.BbsBookDao;
import com.hermit.system.dao.BbsLinkMemberBookDao;
import com.hermit.system.entity.BbsBookEntity;
import com.hermit.system.entity.BbsLinkMemberBookEntity;
import com.hermit.system.entity.BbsMemberEntity;
import com.hermit.system.service.BbsBookService;
import com.hermit.system.service.BbsLinkMemberBookService;
import com.hermit.system.service.BbsMemberService;
import org.omg.CORBA.INTERNAL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("bbsBookService")
public class BbsBookServiceImpl extends ServiceImpl<BbsBookDao, BbsBookEntity> implements BbsBookService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private BbsMemberService bbsMemberService;
    @Autowired
    private BbsBookService bbsBookService;
    @Autowired
    private BbsLinkMemberBookService bbsLinkMemberBookService;
    @Autowired
    private BbsLinkMemberBookDao bbsLinkMemberBookDao;

    /**
    * 查询数据库所有表
    */
    public List<BbsBookEntity> queryAll () {
        List<BbsBookEntity> allDataList = baseMapper.queryAll();
        System.out.println("查询结果:" + allDataList);
        return allDataList;
    }

    /**
    * 删除
    */
    public void delete (int id) {
        BbsBookEntity bbsBookEntity = baseMapper.selectById(id);
        bbsBookEntity.setDeleteFlag(0);
        baseMapper.updateById(bbsBookEntity);
    }

    /**
     * 借书
     */
    public String borrow(BbsBookEntity bbsBookEntity) {
        // 查询用户信息
        BbsMemberEntity bbsMemberEntity = bbsMemberService.getById(bbsBookEntity.getMemberId());
        if (bbsMemberEntity == null) {
            System.out.println("会员不存在");
            return "会员不存在";
        }
        if (bbsBookEntity.getAvailableNumber() < 1) {
            System.out.println("目前暂无可借阅书籍");
            return "目前暂无可借阅书籍";
        }
        QueryWrapper<BbsLinkMemberBookEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", bbsBookEntity.getMemberId());
        queryWrapper.eq("book_id",bbsBookEntity.getId());
        queryWrapper.eq("status", 0);
        queryWrapper.eq("delete_flag", 0);
        List<BbsLinkMemberBookEntity> bbsLinkMemberBookEntities = bbsLinkMemberBookDao.query(queryWrapper);
        if (bbsLinkMemberBookEntities.size() != 0) {
            System.out.println("你已经借过这本书啦");
            return "你已经借过这本书啦";
        }
        // 修改数据剩余库存
        bbsBookEntity.setAvailableNumber(bbsBookEntity.getAvailableNumber() - 1);
        bbsBookEntity.setUpdatedTime(new Date());
        bbsBookService.updateById(bbsBookEntity);
        // 新加用户借书记录
        BbsLinkMemberBookEntity bbsLinkMemberBookEntity = new BbsLinkMemberBookEntity();
        bbsLinkMemberBookEntity.setMemberId(bbsMemberEntity.getId());
        bbsLinkMemberBookEntity.setBookId(bbsBookEntity.getId());
        bbsLinkMemberBookEntity.setDeleteFlag(0);
        bbsLinkMemberBookEntity.setCreatedAt(new Date());
        bbsLinkMemberBookEntity.setUpdatedAt(new Date());
        bbsLinkMemberBookEntity.setStatus(0);
        bbsLinkMemberBookService.save(bbsLinkMemberBookEntity);
        return "借阅成功";
    }

    /**
     * 还书
     */
    public String returnBook(BbsBookEntity bbsBookEntity) {
        // 查询借阅记录
        QueryWrapper<BbsLinkMemberBookEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", bbsBookEntity.getMemberId());
        queryWrapper.eq("book_id",bbsBookEntity.getId());
        queryWrapper.eq("status", 0);
        queryWrapper.eq("delete_flag", 0);
        List<BbsLinkMemberBookEntity> bbsLinkMemberBookEntities = bbsLinkMemberBookDao.query(queryWrapper);
        if(bbsLinkMemberBookEntities.size() == 0) {
            System.out.println("你并没有借这本书哦");
            return "你并没有借这本书哦";
        }
        BbsLinkMemberBookEntity bbsLinkMemberBookEntity = bbsLinkMemberBookEntities.get(0);
        bbsLinkMemberBookEntity.setStatus(1);
        bbsLinkMemberBookEntity.setUpdatedAt(new Date());
        bbsLinkMemberBookService.updateById(bbsLinkMemberBookEntity);
        bbsBookEntity.setUpdatedTime(new Date());
        bbsBookEntity.setAvailableNumber(bbsBookEntity.getAvailableNumber() + 1);
        bbsBookService.updateById(bbsBookEntity);
        return "还书成功";
    }

    /**
     * 查询我的借书记录
     */
    public List<BbsBookEntity> queryRecord(int id) {
        QueryWrapper<BbsBookEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book.delete_flag", 0);
        queryWrapper.eq("member.id", id);
        return baseMapper.queryRecord(queryWrapper);
    }
}